import { ModalForm, ProFormText, ProFormDigit, ProFormRadio, ProFormTextArea } from '@ant-design/pro-components';
import { Form } from 'antd';
import React from 'react';

interface QuestionTypeFormProps {
  trigger: React.ReactNode;
  onFinish: (values: API.IQuestionType) => Promise<boolean>;
  initialValues?: API.IQuestionType;
  formType: 'create' | 'edit';
}

const QuestionTypeForm: React.FC<QuestionTypeFormProps> = ({
                                                             trigger,
                                                             onFinish,
                                                             initialValues,
                                                             formType
                                                           }) => {
  const [form] = Form.useForm();

  return (
    <ModalForm<API.IQuestionType>
      title={formType === 'create' ? '新增题目类别' : '编辑题目类别'}
      width={600}
      trigger={trigger as any}
      form={form}
      autoFocusFirstInput
      onFinish={async (values) => {
        const success = await onFinish(values);
        if (success) {
          form.resetFields();
        }
        return success;
      }}
      initialValues={initialValues || {
        status: "0",
        sortOrder: 0,
      }}
    >
      <ProFormText
        name="name"
        label="类别名称"
        placeholder="请输入类别名称"
        rules={[{ required: true, message: '请输入类别名称' }]}
      />

      <ProFormText
        name="code"
        label="类别编码"
        placeholder="请输入类别编码"
        rules={[{ required: true, message: '请输入类别编码' }]}
      />

      <ProFormTextArea
        name="description"
        label="类别描述"
        placeholder="请输入类别描述"
      />

      <ProFormDigit
        name="sortOrder"
        label="排序权重"
        min={0}
        fieldProps={{ precision: 0 }}
      />

      <ProFormRadio.Group
        name="status"
        label="状态"
        options={[
          { label: '启用', value: "0" },
          { label: '禁用', value: "1" },
        ]}
        rules={[{ required: true, message: '请选择状态' }]}
      />
    </ModalForm>
  );
};

export default QuestionTypeForm;
